//
//  OneOrderViewController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/31.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class OneOrderViewController: BaseViewController,OrderFooterCellDeleage {
    
    var dataModel : OrderDataModel?
    
    var page = -999
    
    var orderType = "-999"

    override func viewDidLoad() {
        super.viewDidLoad()
        
        groupTableView.register(UINib(nibName: StringFromClass(classType: OrderNormalCell.self), bundle: nil), forCellReuseIdentifier: OrderNormalCell.identifer())
        
        groupTableView.register(UINib(nibName: StringFromClass(classType: OrderFooterCell.self), bundle: nil), forCellReuseIdentifier: OrderFooterCell.identifer())

        
        groupTableView.register(OrderListHeaderView.self, forHeaderFooterViewReuseIdentifier: OrderListHeaderView.identifer())
        
        
        groupTableView.rowHeight = UITableViewAutomaticDimension
        
        groupTableView.estimatedRowHeight = 100
        
        groupTableView.contentInset = UIEdgeInsetsMake(ZMNavMaxY + ZMTitlesViewH, 0, 0, 0)
        
        
        weak var weakSelf = self
        
        groupTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadOrderData()
            
        }
        
        groupTableView.mj_header.beginRefreshing()
        
        groupTableView.mj_footer = ZMRefreshFooter.init{
            
            
            weakSelf!.loadMoreOrderData()
            
        }
        

        
        // Do any additional setup after loading the view.
    }
    
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}

extension OneOrderViewController{

    func loadOrderData(){
        
        page = 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        
        parmeter["state_type"] = orderType as AnyObject
        
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_order&a=order_list", parameters: parmeter) { [weak self] (responseObject, error) in
            
            
            self!.groupTableView.mj_header.endRefreshing()

            
            if error == nil{
                
                guard let respons = responseObject else{
                    
                    SVProgressHUD.showError(withStatus: "数据解析错误!")
                    return
                }
                
                self?.dataModel = OrderDataModel(dic: respons)
                
                if self?.dataModel == nil {
                    
                    SVProgressHUD.showError(withStatus: "数据解析错误!")
                    
                    return
                }
                
                
                guard let array = respons["datas"]?["order_list"] as? [[String : AnyObject]] else{
                    
                    SVProgressHUD.showError(withStatus: "数据解析错误!")
                    
                    return
                    
                }
                
                
                
                
                for item in array{
                    
                    self?.dataModel?.order_list.append(OneOrderModel(dic: item))
                    
                    
                }
                
                
              
                
                
                if self?.dataModel?.order_list.count == 0{
                    
                    self?.showLogoView(text : "暂无相关订单...")
                    
                    
                }else{
                
                   self!.groupTableView.mj_footer.resetNoMoreData()
                    
                    if self?.dataModel?.hasmore == "0" {
                        
                        self!.groupTableView.mj_footer.endRefreshingWithNoMoreData()
                        
                        
                    }
                
                }

                
                self?.groupTableView.reloadData()
                
                
            
            }else{
            
                self!.groupTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)

            
            
            }
            
            
            
        }
        
        
        
    }
    
    
    func loadMoreOrderData(){
        
        
        page += 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        
        parmeter["state_type"] = orderType as AnyObject
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_order&a=order_list", parameters: parmeter) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                
                guard let respons = responseObject else{
                    
                    SVProgressHUD.showError(withStatus: "数据解析错误!")
                    return
                }
                
                

                
                guard let array = respons["datas"]?["order_list"] as? [[String : AnyObject]] else{
                    
                    SVProgressHUD.showError(withStatus: "数据解析错误!")
                    
                    return
                    
                }
                
                
                
                
                for item in array{
                    
                    self?.dataModel?.order_list.append(OneOrderModel(dic: item))
                    
                    
                }
                
                
                
                
                if self?.dataModel?.hasmore == "0" {
                    
                    self!.groupTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                    
                }else{
                    
                    self!.groupTableView.mj_footer.endRefreshing()
                    
                }
                
                
                
                
                self!.groupTableView.reloadData()
                
                
            }else{
                
                self!.groupTableView.mj_footer.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
    }
    

        
        
        
        
}








// MARK:- tableview代理方法
extension OneOrderViewController{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
        
        return dataModel!.order_list.count
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
        
        guard let oneOrder = dataModel?.order_list[section] else {
            
            return 0
        }
        
        return oneOrder.extend_order_goods.count + 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var tableCell : UITableViewCell? = nil
        
        let oneOrder = dataModel?.order_list[indexPath.section]
        
        if indexPath.row == oneOrder?.extend_order_goods.count {
            
            let temp = tableView.dequeueReusableCell(withIdentifier: OrderFooterCell.identifer()) as! OrderFooterCell
            
            if oneOrder != nil {
                
                temp.model = oneOrder!
                
                temp.delegate = self

            }
            
            
            tableCell = temp
            

        } else {
            
            let temp = tableView.dequeueReusableCell(withIdentifier: OrderNormalCell.identifer()) as! OrderNormalCell
            
            temp.model = oneOrder?.extend_order_goods[indexPath.row]
            
            tableCell = temp
            
        }
        
        
        
        return tableCell!
    }
    
    
      
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let oneOrder = dataModel?.order_list[indexPath.section]

        if indexPath.row == oneOrder?.extend_order_goods.count {
            
            return
            
        } else {
            
            let temp = oneOrder?.extend_order_goods[indexPath.row]
            
            
            let tempV = UpLoadIconController()
            
            
            if let str = temp?.detail_url {
                
                tempV.urlStr = str
                
                
                navigationController?.pushViewController(tempV, animated: true)

            }
            
            
            
            
        }
        
        
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if dataModel == nil {
            return 0
        }
        
        let oneOrder = dataModel?.order_list[indexPath.section]
        
        if indexPath.row == oneOrder?.extend_order_goods.count {
            
            
            if indexPath.section == dataModel!.order_list.count - 1 {
                
                return  76

            } else {
                
                return  82

            }
            
        
        }else{
        
          
              return 106
            
        }
        
        
        
        }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 36
        
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 0.00001
        
    }
    
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let oneOrder = dataModel?.order_list[section]
        
        let headerView =  tableView.dequeueReusableHeaderFooterView(withIdentifier: OrderListHeaderView.identifer()) as! OrderListHeaderView
        
        
        headerView.leftStr = oneOrder?.store_name
        
        headerView.rightStr = oneOrder?.state_desc
        
        
        return headerView
        

    }
    
}


extension OneOrderViewController{


    func orderFooterCellBtnDidTap(str: String) {
        
        QL1(str)
    }


}



